package com.dengdai.applibrary.widgets.custom;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dengdai.applibrary.config.ResIdName;
import com.dengdai.applibrary.config.ViewInfo;
import com.dengdai.applibrary.utils.BaseViewUtils;

/**
 * @说明 加载界面
 * @作者 LY
 * @时间 16/5/18 16:59
 * @邮箱 2743569843@qq.com
 * @版权 Copyright(c) 2016 LY-版权所有
 * @备注
 */
public class LoadDataEmptyView extends FrameLayout {
    private Context mContext;
    private LinearLayout lineLoading;
    private LinearLayout lineNetError;
    private LinearLayout lineLoadError;
    private TextView tvLoading;
    private ImageView ivNetWork;
    private TextView tvNetInfo;
    private ImageView ivLoadError;
    private TextView tvLoadError;

    public LoadDataEmptyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
    }

    /**
     * 初始化界面
     */
    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(BaseViewUtils.gainResId(mContext, BaseViewUtils.LAYOUT, ViewInfo.LOAD_VIEW), null);
        lineLoading = (LinearLayout) view.findViewById(BaseViewUtils.gainResId(mContext, BaseViewUtils.ID, ResIdName.LINELOADING));
        lineNetError = (LinearLayout) view.findViewById(BaseViewUtils.gainResId(mContext, BaseViewUtils.ID, ResIdName.LINENETERROR));
        lineLoadError = (LinearLayout) view.findViewById(BaseViewUtils.gainResId(mContext, BaseViewUtils.ID, ResIdName.LINELOADERROR));
        tvLoading = (TextView) view.findViewById(BaseViewUtils.gainResId(mContext, BaseViewUtils.ID, ResIdName.TVLOADING));
        ivNetWork = (ImageView) view.findViewById(BaseViewUtils.gainResId(mContext, BaseViewUtils.ID, ResIdName.IVNETWORK));
        tvNetInfo = (TextView) view.findViewById(BaseViewUtils.gainResId(mContext, BaseViewUtils.ID, ResIdName.TVNETINFO));
        ivLoadError = (ImageView) view.findViewById(BaseViewUtils.gainResId(mContext, BaseViewUtils.ID, ResIdName.IVLOADERROR));
        tvLoadError = (TextView) view.findViewById(BaseViewUtils.gainResId(mContext, BaseViewUtils.ID, ResIdName.TVLOADERROR));
        addView(view);
    }

    /**
     * 数据加载中...
     */
    public void loadingData() {
        loadingData(null);
    }

    /**
     * 数据加载中...
     *
     * @param loadingInfo 加载信息
     */
    public void loadingData(String loadingInfo) {
        lineLoading.setVisibility(VISIBLE);
        lineNetError.setVisibility(GONE);
        lineLoadError.setVisibility(GONE);
        if (TextUtils.isEmpty(loadingInfo)) {
            tvLoading.setText(loadingInfo);
        }
    }

    /**
     * 网络错误...
     */
    public void netWorkError() {
        netWorkError(0, null);
    }


    /**
     * 网络错误...
     *
     * @param resId       图片资源
     * @param netWorkInfo 网络错误信息
     */
    public void netWorkError(int resId, String netWorkInfo) {
        lineLoading.setVisibility(GONE);
        lineNetError.setVisibility(VISIBLE);
        lineLoadError.setVisibility(GONE);
        if (TextUtils.isEmpty(netWorkInfo)) {
            tvNetInfo.setText(netWorkInfo);
        }
        if (resId > 0) {
            ivNetWork.setImageResource(resId);
        }
    }

    /**
     * 加载数据错误信息
     */
    public void loadDataError() {
        loadDataError(0, null);
    }

    /**
     * 加载数据错误信息
     *
     * @param resId         图片资源
     * @param loadErrorInfo 错误信息
     */
    public void loadDataError(int resId, String loadErrorInfo) {
        lineLoading.setVisibility(GONE);
        lineNetError.setVisibility(GONE);
        lineLoadError.setVisibility(VISIBLE);
        if (TextUtils.isEmpty(loadErrorInfo)) {
            tvLoadError.setText(loadErrorInfo);
        }
        if (resId > 0) {
            ivLoadError.setImageResource(resId);
        }
    }


}
